import { createRouter, createWebHashHistory } from 'vue-router'
import { getSession } from '@/utils/local'
import { ElMessage } from 'element-plus'

const routes = [
    {
        path: '/login',
        name: 'Login',
        component: () => import('@/views/Login.vue')
    },
    {
        path: '/',
        name: 'Layout',
        component: () => import('@/views/layout/index.vue'),
        children:[
            {
                path: '/',
                name: 'Home',
                component: () => import('@/views/Home.vue')
            },
            {
                path: '/file',
                name: 'file',
                component: () => import('@/views/File.vue')
            },
            {
                path: '/tree',
                name: 'tree',
                component: () => import('@/views/Tree.vue')
            },
            {
                path: '/detail/:id',
                name: 'detail',
                component: () => import('@/views/Detail.vue')
            },
            {
                path: '/upload',
                name: 'upload',
                component: () => import('@/views/Upload.vue')
            },
            {
                path: '/user',
                name: 'user',
                component: () => import('@/views/User.vue')
            },
            {
                path: '/:catchAll(.*)',
                name: '/404',
                component: () => import('@/views/404.vue')
            }
        ]
    }
]

const router = createRouter({
    history: createWebHashHistory(),
    routes
})

router.beforeEach((to, from, next) => {
    const token = getSession('token')
    if (!token && to.name != 'Login') {
        next({ name: 'Login' })
        // ElMessage.warning('抱歉，没有检测到凭证信息')
  } else {
    next()
  }
})

export default router
